using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using CMS.Application.Common.Interface;
using CMS.Domain.Entity;
using Microsoft.AspNetCore.Mvc.Filters;

namespace CMS.Infrastructrue.Filters
{
    public class HttpGlobalExceptionFilter : IAsyncExceptionFilter
    {
        private readonly IRepository<CMSExceptionLog> _CMSExceptionLogRes;

        public HttpGlobalExceptionFilter(IRepository<CMSExceptionLog> CMSExceptionLogRes)
        {
            _CMSExceptionLogRes = CMSExceptionLogRes;
        }


        public async Task OnExceptionAsync(ExceptionContext context)
        {
            //捕获全局异常，记入数据库日志
            await _CMSExceptionLogRes.AddAsync(new CMSExceptionLog
            {
                ShortMessage = context.Exception.Message,
                FullMessage = context.Exception.ToString()
            });

            context.ExceptionHandled = true;
        }
    }
}